



(function(Global /*, context */) {
    var isInit=false;
    var abilitiesCol=6;
    var abilitiesELE=Global.Panel.dotaHud.FindChildTraverse("abilities");
    var dragAbilityIndex=null;

    function OnDragDrop(panel,draggedPanel) {
        panel.SetDraggable(true);
        // $.Msg( "OnDragDrop..", panel,draggedPanel)
        if (dragAbilityIndex!==null){
            // $.Msg( "OnDragDrop..", panel.abilityIndex,draggedPanel.dragToAbilityIndex)
            if(Math.floor(dragAbilityIndex/abilitiesCol)===Math.floor(panel.abilityIndex/abilitiesCol)){
                GameEvents.SendCustomGameEventToServer("drag_ability", { "playerId": Game.GetLocalPlayerID(),"from": dragAbilityIndex,"to":panel.abilityIndex});
            }
        }
        dragAbilityIndex=null
        return true;
    }

    function OnDragEnter( panel, draggedPanel ) {
        panel.style.opacity="0.2";
        // $.Msg("OnDragEnter...",panel,panel.abilityIndex);
        return true;
    }

    function OnDragLeave(panel,draggedPanel) {
        panel.style.opacity="1"
        // $.Msg("OnDragLeave...!!!",panel)
        return true;
    }
    
    function OnDragEnd( panel,draggedPanel ){
        draggedPanel.DeleteAsync( 0 );
        // $.Msg("OnDragEnd...!!!",panel)
        panel.SetDraggable(true);
        dragAbilityIndex=null
        return true;
    }

    function OnDragStart( panel,dragCallbacks )
    {
        var localHeroIndex = Players.GetPlayerHeroEntityIndex( Players.GetLocalPlayer());
        var abilityIndex = Entities.GetAbility( localHeroIndex ,panel.abilityIndex);
        var abilityName=Abilities.GetAbilityName( abilityIndex);

        if(abilityName==="ability_empty"){
            return false;
        }

        $.DispatchEvent('DOTAHideAbilityTooltip');
        var displayPanel = $.CreatePanel( "DOTAAbilityImage", abilitiesELE, "dragImage" );
        // $.Msg("OnDragStart...!!!",panel,panel.abilityIndex,abilityName);
        panel.SetDraggable(false);
        dragAbilityIndex=panel.abilityIndex;
        displayPanel.abilityname = abilityName;
        dragCallbacks.displayPanel = displayPanel;
        dragCallbacks.offsetX = 0;
        dragCallbacks.offsetY = 0;
        return true;
    }


    function Init(){

      if((isInit===false&&(abilitiesELE.FindChildTraverse("Ability"+(abilitiesCol*3-1))!==null))) {
          isInit = true;
          Global.Panel.dotaHud.FindChildTraverse("center_block").style.height = "400px"
          Global.Panel.dotaHud.FindChildTraverse("center_block").FindChildTraverse("center_bg").style.backgroundColor=Global.Config.abilityBasicBgColor;
          abilitiesELE.style["flow-children"] = "down";
          abilitiesELE.style.height = "225px";
          abilitiesELE.style.marginBottom = "0px";
          // abilitiesELE.style.backgroundImage="url(s2r://panorama/images/hud/reborn/ability_bg_psd.png)"
          var attrContainer = abilitiesELE.FindChildTraverse("AbilitiesAttrContainer");
          var basContainer = abilitiesELE.FindChildTraverse("AbilitiesBasContainer");
          var ultContainer = abilitiesELE.FindChildTraverse("AbilitiesUltContainer");


          if (attrContainer === null) {
              attrContainer = $.CreatePanel("Panel", abilitiesELE, "AbilitiesAttrContainer")
          }
          if (ultContainer === null) {
              ultContainer = $.CreatePanel("Panel", abilitiesELE, "AbilitiesUltContainer")
          }
          if (basContainer === null) {
              basContainer = $.CreatePanel("Panel", abilitiesELE, "AbilitiesBasContainer")
          }
          ultContainer.style.backgroundColor=Global.Config.abilityUltBgColor;
          ultContainer.style.backgroundImage = "url(s2r://panorama/images/hud/reborn/ability_bg_psd.vtex)"
          ultContainer.style.backgroundSize = "100%";
          attrContainer.style.backgroundColor=Global.Config.abilityAttrBgColor;
          attrContainer.style.backgroundImage = "url(s2r://panorama/images/hud/reborn/ability_bg_psd.vtex)"
          attrContainer.style.backgroundSize = "100%";

          attrContainer.style["flow-children"] = "right";
          basContainer.style["flow-children"] = "right";
          ultContainer.style["flow-children"] = "right";
          attrContainer.style.height="67px";
          basContainer.style.height="67px";
          ultContainer.style.height="67px";

          for (var i = 0; i < abilitiesCol; i++) {
              var ability = abilitiesELE.FindChildTraverse("Ability" + i);
              var abilityBtn
              if (ability !== null) {
                  ability.SetParent(basContainer)

                  abilityBtn=ability.FindChildTraverse("AbilityButton").FindChildTraverse("AbilityImage");
                  abilityBtn.SetDraggable(true);
                  abilityBtn.abilityIndex=i;
                  $.RegisterEventHandler( 'DragEnter', abilityBtn, OnDragEnter);
                  $.RegisterEventHandler( 'DragStart', abilityBtn, OnDragStart);
                  $.RegisterEventHandler( 'DragEnd', abilityBtn, OnDragEnd);
                  $.RegisterEventHandler( 'DragLeave', abilityBtn, OnDragLeave);
                  $.RegisterEventHandler( 'DragDrop', abilityBtn, OnDragDrop);
              }
              ability = abilitiesELE.FindChildTraverse("Ability" + (i + abilitiesCol));
              if (ability !== null) {
                  ability.SetParent(attrContainer)
                  abilityBtn=ability.FindChildTraverse("AbilityButton").FindChildTraverse("AbilityImage");
                  abilityBtn.SetDraggable(true);
                  abilityBtn.abilityIndex= (i + abilitiesCol);
                  $.RegisterEventHandler( 'DragEnter', abilityBtn, OnDragEnter);
                  $.RegisterEventHandler( 'DragStart', abilityBtn, OnDragStart);
                  $.RegisterEventHandler( 'DragEnd', abilityBtn, OnDragEnd);
                  $.RegisterEventHandler( 'DragLeave', abilityBtn, OnDragLeave);
                  $.RegisterEventHandler( 'DragDrop', abilityBtn, OnDragDrop);

              }
              ability = abilitiesELE.FindChildTraverse("Ability" + (i + abilitiesCol * 2));
              if (ability !== null) {
                  ability.SetParent(ultContainer)
                  abilityBtn=ability.FindChildTraverse("AbilityButton").FindChildTraverse("AbilityImage");
                  abilityBtn.SetDraggable(true);
                  abilityBtn.abilityIndex= (i + abilitiesCol * 2);
                  $.RegisterEventHandler( 'DragEnter', abilityBtn, OnDragEnter);
                  $.RegisterEventHandler( 'DragStart', abilityBtn, OnDragStart);
                  $.RegisterEventHandler( 'DragEnd', abilityBtn, OnDragEnd);
                  $.RegisterEventHandler( 'DragLeave', abilityBtn, OnDragLeave);
                  $.RegisterEventHandler( 'DragDrop', abilityBtn, OnDragDrop);
              }
              ability.FindChildTraverse("Hotkey").style.visibility = "visible";
              switch (i) {
                  case 0:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "Z");
                      break;
                  case 1:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "X");
                      break;
                  case 2:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "C");
                      break;
                  case 3:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "V");
                      break;
                  case 4:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "B");
                      break;
                  case 5:
                      ability.FindChildTraverse("HotkeyText").SetDialogVariable("hotkey", "N");
                      break;
              }
          }
          var abilityELE
          for (var i = 0; i < Global.GameConfig.abilitiesSlotCount; i++) {
              abilityELE=abilitiesELE.FindChildTraverse("Ability"+i);
              abilityELE.FindChildTraverse("AbilityLevelContainer").style.visibility="collapse";
              abilityELE.FindChildTraverse("AbilityLevelContainer").visible=false;
              abilityELE.FindChildTraverse("ButtonWithLevelUpTab").style.width="64px";
              abilityELE.FindChildTraverse("ButtonWithLevelUpTab").style.height="64px";
              abilityELE.FindChildTraverse("ButtonWithLevelUpTab").style.marginBottom="3px";
              abilityELE.FindChildTraverse("ButtonWithLevelUpTab").style.marginTop="3px";
              abilityELE.FindChildTraverse("ButtonSize").style.width="64px";
              abilityELE.FindChildTraverse("ButtonSize").style.height="64px";
              abilityELE.FindChildTraverse("ButtonWell").style.width="64px";
              abilityELE.FindChildTraverse("ButtonWell").style.height="64px";

          }
      }
      else {
          $.Schedule(0.1,Init);
      }

  }


   Init();

})(GameUI.CustomUIConfig(), this);